;;; Copyright © 2020 Maxime Devos <maximedevos@telenet.be>
;;; This file is part of rehash.
;;;
;;; rehash is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; rehash is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with rehash.  If not, see <http://www.gnu.org/licenses/>.
(use-modules (remirror recognise-hash-uri)
	     (remirror base16))

(define (%narinfo/0 common)
  `(unknown-narinfo ,common "a462kby1q51ndvxdv3b6p0rsixxrgx1h"))
(define %narinfo-uris
  `(("/a462kby1q51ndvxdv3b6p0rsixxrgx1h.narinfo"
     . ,(%narinfo/0 "/"))
    ;; These two were downloadable at time of writing
    ("http://ci.guix.gnu.org/a462kby1q51ndvxdv3b6p0rsixxrgx1h.narinfo"
     . ,(%narinfo/0 "http://ci.guix.gnu.org/"))
    ("http://ci.guix.gnu.org/a462kby1q51ndvxdv3b6p0rsixxrgx1h.narinfo"
     . ,(%narinfo/0 "http://ci.guix.gnu.org/"))))

(define %content-addressed/0
  `(sha256
    ,(base16-string->bytevector
      "31e066137a962676e89f69d1b65382de95a7ef7d914b8cb956f41ea72e0f516b")))

(define %content-addressed
  ;; This one was downloadable at time of writing
  `(("http://localhost:8080/file/hello-2.10.tar.gz/sha256/0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i"
     . ,%content-addressed/0)
    ("/sha256/0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i"
     . ,%content-addressed/0)))

(define %swh-sha256/0
  `(sha256
    ,(base16-string->bytevector "92d0fa1c311cacefa89853bdb53c62f4110cdfda3820346b59cbd098f40f955e")))

(define %some-swh-sha256
  `(("https://archive.softwareheritage.org/api/1/content/sha256:92d0fa1c311cacefa89853bdb53c62f4110cdfda3820346b59cbd098f40f955e/raw"
     . ,%swh-sha256/0)
    ;; an alternative archive is also ok
    ("sha256:92d0fa1c311cacefa89853bdb53c62f4110cdfda3820346b59cbd098f40f955e/raw"
     . ,%swh-sha256/0)
    ("/sha256:92d0fa1c311cacefa89853bdb53c62f4110cdfda3820346b59cbd098f40f955e/raw"
     . ,%swh-sha256/0)))

(define %nar-uris
  ;; Here's an uncompressed one
  '(("http://localhost:8080/nar/a462kby1q51ndvxdv3b6p0rsixxrgx1h-hello-2.10"
     unknown-substitute "http://localhost:8080/"
     "none" "a462kby1q51ndvxdv3b6p0rsixxrgx1h" "hello-2.10")
    ("/nar/gzip/a462kby1q51ndvxdv3b6p0rsixxrgx1h-hello-2.10"
     unknown-substitute "/"
     "gzip" "a462kby1q51ndvxdv3b6p0rsixxrgx1h" "hello-2.10")
    ("/nar/lzip/a462kby1q51ndvxdv3b6p0rsixxrgx1h-hello-2.10"
     unknown-substitute "/"
     "lzip" "a462kby1q51ndvxdv3b6p0rsixxrgx1h" "hello-2.10")))

(define (test-set nom set try?)
  (define counter 1)
  (define count (length set))
  (for-each (lambda (uri+parsed)
	      (test-equal (format #f "~a ~a/~a" nom counter count)
		(cdr uri+parsed)
		(try? (car uri+parsed)))
	      (set! counter (1+ counter)))
	    set))

(test-set "parse narinfo URI" %narinfo-uris narinfo-url?)
(test-set "parse content-addressed URI"
	  %content-addressed sha256/tarballs.nixos.org?)
(test-set "nar url" %nar-uris substitute-url?)
(test-set "parse Software Heritage sha256 raw"
	  %some-swh-sha256 sha256/swh?)

;; TODO test what happens on bad input
